package com.zdb.n1.controller;

import com.zdb.n1.entity.HealthData;
import com.zdb.n1.entity.User;
import com.zdb.n1.payload.response.MessageResponse;
import com.zdb.n1.service.HealthDataService;
import com.zdb.n1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/health-data")
@PreAuthorize("hasRole('ADMIN')")
public class AdminHealthDataController {

    @Autowired
    private HealthDataService healthDataService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 获取指定用户的所有健康数据
     * @param userId 用户ID
     * @return 健康数据列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<HealthData>> getUserHealthData(@PathVariable Long userId) {
        User user = userService.getUserById(userId);
        List<HealthData> healthDataList = healthDataService.getHealthDataByUser(user);
        return ResponseEntity.ok(healthDataList);
    }
    
    /**
     * 获取指定用户的特定健康数据记录
     * @param userId 用户ID
     * @param dataId 健康数据ID
     * @return 健康数据记录
     */
    @GetMapping("/user/{userId}/{dataId}")
    public ResponseEntity<HealthData> getUserHealthDataById(@PathVariable Long userId, @PathVariable Long dataId) {
        User user = userService.getUserById(userId);
        HealthData healthData = healthDataService.getHealthDataById(dataId);
        
        // 验证健康数据属于指定用户
        if (!healthData.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健康数据不属于指定用户");
        }
        
        return ResponseEntity.ok(healthData);
    }
    
    /**
     * 更新用户的健康数据
     * @param userId 用户ID
     * @param dataId 健康数据ID
     * @param healthData 更新的健康数据
     * @return 更新后的健康数据
     */
    @PutMapping("/user/{userId}/{dataId}")
    public ResponseEntity<HealthData> updateUserHealthData(
            @PathVariable Long userId,
            @PathVariable Long dataId,
            @RequestBody HealthData healthData) {
        
        User user = userService.getUserById(userId);
        HealthData existingData = healthDataService.getHealthDataById(dataId);
        
        // 验证健康数据属于指定用户
        if (!existingData.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健康数据不属于指定用户");
        }
        
        // 设置必要的字段
        healthData.setId(dataId);
        healthData.setUser(user);
        
        // 更新并返回结果
        HealthData updatedData = healthDataService.updateHealthData(dataId, healthData);
        return ResponseEntity.ok(updatedData);
    }
    
    /**
     * 删除用户的健康数据
     * @param userId 用户ID
     * @param dataId 健康数据ID
     * @return 操作结果消息
     */
    @DeleteMapping("/user/{userId}/{dataId}")
    public ResponseEntity<MessageResponse> deleteUserHealthData(
            @PathVariable Long userId,
            @PathVariable Long dataId) {
        
        User user = userService.getUserById(userId);
        HealthData existingData = healthDataService.getHealthDataById(dataId);
        
        // 验证健康数据属于指定用户
        if (!existingData.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健康数据不属于指定用户");
        }
        
        // 删除并返回结果
        healthDataService.deleteHealthData(dataId);
        return ResponseEntity.ok(MessageResponse.success("健康数据删除成功"));
    }
} 